<template>
    <div class='address'>
        <van-nav-bar title="编辑地址" left-arrow @click-left="backUrl" />
        <div class="content">
            <van-address-edit :address-info="AddressInfo" :area-list="areaList" show-delete show-set-default
                show-search-result :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']"
                @save="onSave" @delete="onDelete"/>
        </div>
    </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { areaList } from '@vant/area-data'
import request from '@/utils/request'

export default {
  name: '',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data () {
  // 这里存放数据
    return {
      baseAddress: '/address-book',
      areaList,
      searchResult: [],
      AddressBook: {
        id: 0,
        name: '',
        phone: '',
        provinceCode: '',
        provinceName: '',
        cityName: '',
        districtName: '',
        detail: '',
        isDefault: false
      },
      AddressInfo: {
        id: 0,
        name: '',
        tel: '',
        province: '',
        city: '',
        country: '',
        addressDetail: '',
        areaCode: '',
        isDefault: false
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    // 返回地址列表界面
    backUrl () {
      this.$router.push({
        name: 'AddressBook'
      })
    },
    onSave (content) {
      this.addressTrans(content)
      request({
        url: this.baseAddress + '/address-book',
        method: 'put',
        data: this.AddressBook
      }).then(() => {
        this.backUrl()
      })
    },
    onDelete () {
      request({
        url: this.baseAddress + `/address-book/${this.AddressBook.id}`,
        method: 'delete'
      }).then(() => {
        this.backUrl()
      })
    },
    addressTrans (content) {
      this.AddressBook.name = content.name
      this.AddressBook.phone = content.tel
      this.AddressBook.provinceCode = content.areaCode
      this.AddressBook.provinceName = content.province
      this.AddressBook.cityName = content.city
      this.AddressBook.districtName = content.country
      this.AddressBook.detail = content.addressDetail
      this.AddressBook.isDefault = content.isDefault
    },
    // 回显数据
    async getAddressBookById (id) {
      await request({
        url: this.baseAddress + `/address-book/${id}`,
        method: 'get'
      }).then(res => {
        this.AddressBook = res.data.data
        this.transfAddress(res.data.data)
      })
    },
    transfAddress (data) {
      this.AddressInfo.id = data.id
      this.AddressInfo.name = data.name
      this.AddressInfo.tel = data.phone
      this.AddressInfo.areaCode = data.provinceCode
      this.AddressInfo.province = data.provinceName
      this.AddressInfo.city = data.cityName
      this.AddressInfo.country = data.districtName
      this.AddressInfo.addressDetail = data.detail
      this.AddressInfo.isDefault = data.isDefault
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    this.AddressBook.id = this.$route.params.item.id
    this.getAddressBookById(this.AddressBook.id)
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {

  },
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="less" scoped>
.address {
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }

  min-height: 100vh;
  background-color: #f7f7f7;
}
</style>
